// template/company/order/orderIndex/orderIndex.js
const globalIp = getApp().globalData.userIp
Page({

  /**
   * 页面的初始数据
   */
  data: {
    projectList: [],
    pageIndex: 1,
    options: []
  },

  prevPage: function() {
    if (this.data.pageIndex === 1) {
      wx.showToast({
        title: '已经到达第一页~',
      })
    } else {
        this.setData({
          pageIndex: this.data.pageIndex - 1
        })
        this.alreadyShow(this.data.options)
    }
  },

  nextPage: function() {
    this.setData({
      pageIndex: this.data.pageIndex + 1
    })
    this.alreadyShow(this.data.options)
  },  

  alreadyShow: function(options){
    var that = this;
    wx.showLoading({
      title: '数据加载中...',
    })
    wx.request({
      url: globalIp + 'home/ordr/execCusInfo',
      data: {
        pageIndex: that.data.pageIndex,
        lineNum: options.lineNum,
        openId: wx.getStorageSync('userInfo').openid,
        cardCode: wx.getStorageSync('userInfo').CardCode
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success: function(res) {
        if (res.data.jsonObject) {
          var data = res.data.jsonObject.data;
          if (data.length > 1) {
            var arr = [];
            var res = [];
            for (var i in data) {
              arr[i] = [];
              for(var j in data[i]) {
                arr[i].push(data[i][j]);
              }
            }
            for (var i = 0; i < arr[0].length; ++i) {
              res[i] = [];
              for (var j = 0; j < arr.length; ++j) {
                res[i][j] = arr[j][i];
              }
            } 
            that.setData({
              projectList: res
            })
          } else {
            that.setData({
              pageIndex: that.data.pageIndex - 1
            })
            wx.showToast({
              title: '已到达最后一页~',
            })
          }
        } else {
          wx.showToast({
            title: '请返回上一页面，重新请求',
            icon: 'none',
            duration: 2000,
          })
        }
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    if (options.lineNum === '1') {
      wx.showToast({
        title: '共同合作不支持点击，请返回上一页面',
        icon: 'none',
        duration: 3000,
      })
    } else {
      this.setData({
        options
      })
      this.alreadyShow(options);
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

})